package com.mobileares.midp.widgets.client.textbox2;

import com.google.gwt.event.dom.client.BlurEvent;
import com.google.gwt.event.dom.client.FocusEvent;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;

/**
 * Created by IntelliJ IDEA.
 * User: Tom
 * Date: 2012-5-21
 * Time: 11:02:29
 * To change this template use File | Settings | File Templates.
 */
public class PasswordTextLabel extends BaseLabelText<Object, TextBox> {
    private PasswordTextBox tb = new PasswordTextBox();

    public PasswordTextLabel(String label) {
        super(label);
        setLabelWidget(tb);
        tb.setStyleName("textbox-input");
    }

    public Object getValue() {
        if (tb.getValue().trim().equals("")) return null;
        else
            return tb.getValue().trim();
    }

    public void setValue(Object ob) {
        Object oldValue = getValue();
        tb.setValue(ob == null ? "" : ob.toString());
        changeValue(getValue(), oldValue);
    }

    @Override
    protected void fireErrorMessage() {
        tb.removeStyleDependentName("focus");
        tb.addStyleDependentName("error");

    }

    protected void fireRemoveErrorMessage() {
        tb.removeStyleDependentName("error");
    }

    @Override
    protected void fireFocus(FocusEvent event) {
        if (isValid())
            tb.removeStyleDependentName("error");
        tb.addStyleDependentName("focus");
    }

    @Override
    protected void fireBlur(BlurEvent event) {
        if (!isValid())
            tb.addStyleDependentName("error");
        tb.removeStyleDependentName("focus");

    }
}
